package me.gacl.util;

import java.io.File;
import java.io.IOException;

public class PathUtil {

	public static void main(String[] args) throws IllegalAccessException, IOException {
		PathUtil p = new PathUtil();
		
		p.getClassLoaderRootPath();
		p.getClassLoaderRootPath2();
		
		p.getClassLoaderPath();
		
		p.getProjRootDir();
		p.getProjRootDir2();
		
		p.getPath();
		
		//获取web资源路径
//		System.out.println(p.getWebClassesPath());
//		System.out.println(p.getWebInfPath());
//		System.out.println(p.getWebRoot());
	}
	
	//1.获取工程资源路径
	public String getClassLoaderRootPath() {//1.获取类加载的根路径
        // 第一种：获取类加载的根路径   D:\git\daotie\daotie\target\classes
        String  classLoaderRootPath= this.getClass().getResource("/").getPath();
        System.out.println(classLoaderRootPath);// /Users/zhuzhijia/Desktop/javaEE-workspace/FileUploadAndDownLoad/build/classes/
        return classLoaderRootPath;
	}
	public String getClassLoaderRootPath2() {//1.获取类加载的根路径
        String  classLoaderRootPath= this.getClass().getResource("/").getPath();
        System.out.println(classLoaderRootPath);// /Users/zhuzhijia/Desktop/javaEE-workspace/FileUploadAndDownLoad/build/classes/
        return classLoaderRootPath;
	}
	
	
	
	public String getClassLoaderPath() {//2.获取当前类的加载目录(全路径)
        // // 获取当getClassLoaderRootPath2前类的所在工程路径; 如果不加“/”  获取当前类的加载目录  D:\git\daotie\daotie\target\classes\my
        String  classWorkPath= this.getClass().getResource("").getPath();
        System.out.println(classWorkPath);// /Users/zhuzhijia/Desktop/javaEE-workspace/FileUploadAndDownLoad/build/classes/me/gacl/util/
        return classWorkPath;
	}
	
	 /*
     * 结果： C:\Documents and Settings\Administrator\workspace\projectName
     * 获取当前工程路径
     */
	public String getProjRootDir() throws IOException {//3.获取工程根目录
        // 第一种：获取工程根目录   /Users/zhuzhijia/Desktop/javaEE-workspace/FileUploadAndDownLoad
		File directory = new File("");// 参数为空
        String projDir = directory.getCanonicalPath();
        System.out.println(projDir);
        return projDir;
	}
	public String getProjRootDir2() {
		// 第二种：获取工程根目录   /Users/zhuzhijia/Desktop/javaEE-workspace/FileUploadAndDownLoad
		System.out.println(System.getProperty("user.dir"));
        return System.getProperty("user.dir");// /Users/zhuzhijia/Desktop/javaEE-workspace/FileUploadAndDownLoad
	}
	
	
	
	public String getPath() {
        // 第五种：  获取所有的类路径 包括jar包的路径
        System.out.println(System.getProperty("java.class.path"));
        return System.getProperty("java.class.path");
	}

	
	
	//2.获取web资源路径
	public String getWebClassesPath() {
		String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
		return path;

	}

	public String getWebInfPath() throws IllegalAccessException {
		String path = getWebClassesPath();
		if (path.indexOf("WEB-INF") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF") + 8);
		} else {
			throw new IllegalAccessException("路径获取错误");
		}
		return path;
	}

	public String getWebRoot() throws IllegalAccessException {
		String path = getWebClassesPath();
		if (path.indexOf("WEB-INF") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF/classes"));
		} else {
			throw new IllegalAccessException("路径获取错误");
		}
		return path;
	}

}
